package com.lin.project.hackathon.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.project.hackathon.common.ErrorCode;
import com.lin.project.hackathon.enums.PostGenderEnum;
import com.lin.project.hackathon.enums.PostReviewStatusEnum;
import com.lin.project.hackathon.exception.BusinessException;
import com.lin.project.hackathon.model.entity.Post;
import com.lin.project.hackathon.service.PostService;
import com.lin.project.hackathon.mapper.PostMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
* @author 林怀旭
* @description 针对表【post(帖子)】的数据库操作Service实现
* @createDate 2023-05-15 10:32:04
*/
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post>
    implements PostService{

    /**
     *
     *
     * @description: 校验
     * @param post
     * @param add 是否为创建校验
     * @return:
     * @author: linhuaixu
     * @time: 2023/5/17 19:44
     */

    @Override
    public void validPost(Post post, boolean add) {
        if(post==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Integer age = post.getAge();
        Integer gender = post.getGender();
        String content = post.getContent();
        String job = post.getJob();
        String place = post.getPlace();
        String education = post.getEducation();
        String loveExp = post.getLoveExp();
        Integer reviewStatus = post.getReviewStatus();
        //创建时，所有参数必须非空
        if(add){
            if(StringUtils.isAnyBlank(content,job,place,education,loveExp) || ObjectUtils.anyNull(age,gender)){
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
        }
        if(StringUtils.isNotBlank(content) && content.length() > 8192){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"内容过长");
        }
        if(reviewStatus!=null && !PostReviewStatusEnum.getValues().contains(reviewStatus)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if(age!=null && (age<18 || age>100)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"年龄不符合要求");
        }
        if(gender!=null && !PostGenderEnum.getValues().contains(gender)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"年龄不符合要求");
        }
    }
}




